package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.service.ShoppingCarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车接口
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //创建一个ShoppingCart
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId()); //设置用户id（通过本地线程）

        //通过这个ShoppingCart查询数据库
        List<ShoppingCart> list = shoppingCarMapper.list(shoppingCart);

        //判断当前加入购物车商品是否已经存在,已经存在，数量加一（update）
        if (list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCarMapper.updateById(cart);
        }

        //不存在，插入数据（insert）
        else {
            //判断此次添加数据是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null){   //添加菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setDishId(dish.getId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
            }
            else if (shoppingCartDTO.getSetmealId() != null) {    //添加套餐
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setDishId(setmeal.getId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            }

            //设置新物品数量为一
            shoppingCart.setNumber(1);
            //设置添加时间
            shoppingCart.setCreateTime(LocalDateTime.now());

            //将新数据插入购物车表
            shoppingCarMapper.insert(shoppingCart);
        }

    }


    /**
     * 通过当前用户id查询购物车数据
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> listByUserId(Long userId) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> shoppingCarts = shoppingCarMapper.list(shoppingCart);

        return shoppingCarts;
    }


    /**
     * 清空购物车
     * @param userId
     */
    @Override
    public void clean(Long userId) {
        shoppingCarMapper.clean(userId);
    }


    /**
     * 在购物车中减少对应购物车数据
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart(); //用于查询数据库的Shoppingcart对象
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //查询数据库并修改对应number
        ShoppingCart cart = shoppingCarMapper.list(shoppingCart).get(0);
        Integer number = cart.getNumber();

        //若当前number = 1，那么点击后就会减少为0，我们执行删除该购物车的操作
        if (number == 1){
            shoppingCarMapper.cleanById(cart);
        }
        //若当前number不等于1，那么正常-1即可
        else {
            cart.setNumber(number - 1);
            shoppingCarMapper.updateById(cart);
        }
    }
}
